package net.huashitong.ssydt.provider.article.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.huashitong.ssydt.PaginationResult;
import net.huashitong.ssydt.article.entity.SsArticleArticle;
import net.huashitong.ssydt.article.entity.vo.ArticleArticleInVO;
import net.huashitong.ssydt.article.entity.vo.ArticleArticleOutVO;
import net.huashitong.ssydt.article.entity.vo.ArticleArticleQueryInVO;
import net.huashitong.ssydt.provider.article.service.SsArticleArticleService;
import net.huashitong.ssydt.provider.web.model.Result;
import net.huashitong.ssydt.provider.web.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 文章接口（后台）
 *
 * @author hemj
 * @version 2017/9/14
 */
@Api(value = "/ssydt/article", tags = {"文章接口（后台）"})
@RestController
@RequestMapping("/ssydt/article")
public class SsArticleArticleController {

	private SsArticleArticleService ssArticleArticleService;

	@Autowired
	public SsArticleArticleController(SsArticleArticleService ssArticleArticleService) {
		this.ssArticleArticleService = ssArticleArticleService;
	}

	@ApiOperation(value = "文章分页查询", notes = "文章分页查询")
	@GetMapping
	public Result<PaginationResult<SsArticleArticle>> paginationQuery(@ModelAttribute ArticleArticleQueryInVO articleArticleQueryInVO) {
		return ResultUtils.getSuccessResults(ssArticleArticleService.paginationQuery(articleArticleQueryInVO));
	}

	@ApiOperation(value = "加载文章信息", notes = "加载文章信息")
	@GetMapping("/{articleId}")
	public Result<ArticleArticleOutVO> get(@PathVariable Long articleId) {
		return ResultUtils.getSuccessResults(ssArticleArticleService.getArticle(articleId));
	}

	@ApiOperation(value = "新增文章", notes = "新增文章")
	@PostMapping
	public Result<Long> post(@Valid @RequestBody ArticleArticleInVO articleArticleInVO) {
		return ResultUtils.getSuccessResults(ssArticleArticleService.addArticle(articleArticleInVO));
	}

	@ApiOperation(value = "修改文章", notes = "修改文章")
	@PutMapping("/{articleId}")
	public Result<String> put(@PathVariable Long articleId, @Valid @RequestBody ArticleArticleInVO articleArticleInVO) {
		articleArticleInVO.setId(articleId);
		ssArticleArticleService.updateArticle(articleArticleInVO);
		return ResultUtils.getSuccessResults();
	}

	@ApiOperation(value = "删除文章", notes = "删除文章")
	@DeleteMapping("/{articleId}")
	public Result<String> put(@PathVariable Long articleId) {
		ssArticleArticleService.deleteArticle(articleId);
		return ResultUtils.getSuccessResults();
	}

}
